package com.castle.fortress.admin.flowable.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.flowable.dto.FlowableTemplateDto;
import com.castle.fortress.admin.flowable.entity.FlowableTemplateEntity;
import com.castle.fortress.admin.flowable.form.service.FlowableFormConfigService;
import com.castle.fortress.admin.flowable.mapper.FlowableTemplateMapper;
import com.castle.fortress.admin.flowable.service.FlowableTemplateService;
import com.castle.fortress.admin.flowable.utils.FlowableUtils;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 流程模板管理表 服务实现类
 *
 * @author castle
 * @since 2022-09-26
 */
@Service
public class FlowableTemplateServiceImpl extends ServiceImpl<FlowableTemplateMapper, FlowableTemplateEntity> implements FlowableTemplateService {
    @Autowired
    private FlowableFormConfigService flowableFormConfigService;
    /**
    * 获取查询条件
    * @param flowableTemplateDto
    * @return
    */
    public QueryWrapper<FlowableTemplateEntity> getWrapper(FlowableTemplateDto flowableTemplateDto){
        QueryWrapper<FlowableTemplateEntity> wrapper= new QueryWrapper();
        if(flowableTemplateDto != null){
            FlowableTemplateEntity flowableTemplateEntity = ConvertUtil.transformObj(flowableTemplateDto,FlowableTemplateEntity.class);
            wrapper.like(StrUtil.isNotEmpty(flowableTemplateEntity.getTitle()),"title",flowableTemplateEntity.getTitle());
            wrapper.eq(flowableTemplateEntity.getStatus() != null,"status",flowableTemplateEntity.getStatus());
            wrapper.eq(StrUtil.isNotEmpty(flowableTemplateEntity.getActKey()),"act_key",flowableTemplateEntity.getActKey());
            wrapper.eq(flowableTemplateEntity.getFormKey() != null,"form_key",flowableTemplateEntity.getFormKey());
        }
        return wrapper;
    }


    @Override
    public IPage<FlowableTemplateDto> pageFlowableTemplate(Page<FlowableTemplateDto> page, FlowableTemplateDto flowableTemplateDto) {
//        QueryWrapper<FlowableTemplateEntity> wrapper = getWrapper(flowableTemplateDto);
//        Page<FlowableTemplateEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
//        Page<FlowableTemplateEntity> flowableTemplatePage=baseMapper.selectPage(pageEntity,wrapper);
//        Page<FlowableTemplateDto> pageDto = new Page(flowableTemplatePage.getCurrent(), flowableTemplatePage.getSize(),flowableTemplatePage.getTotal());
//        pageDto.setRecords(ConvertUtil.transformObjList(flowableTemplatePage.getRecords(),FlowableTemplateDto.class));
//        return pageDto;
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        FlowableTemplateEntity entity = ConvertUtil.transformObj(flowableTemplateDto,FlowableTemplateEntity.class);
        List<FlowableTemplateEntity> list=baseMapper.extendsList(pageMap,entity);
        Long total = baseMapper.extendsCount(entity);
        Page<FlowableTemplateDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(list,FlowableTemplateDto.class));
        return pageDto;
    }


    @Override
    public List<FlowableTemplateDto> listFlowableTemplate(FlowableTemplateDto flowableTemplateDto){
        QueryWrapper<FlowableTemplateEntity> wrapper = getWrapper(flowableTemplateDto);
        List<FlowableTemplateEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,FlowableTemplateDto.class);
    }

    @Override
    public FlowableTemplateDto getByIdExtends(Long id) {
        FlowableTemplateEntity  entity = baseMapper.getByIdExtends(id);
        FlowableTemplateDto dto = ConvertUtil.transformObj(entity,FlowableTemplateDto.class);
        if(dto!=null){
            dto.setForm(flowableFormConfigService.getColsById(dto.getFormKey()));
        }
        return dto;
    }


    @Override
    public void updateXmlData(FlowableTemplateEntity flowableTemplateEntity) {
        String contentXml = "";
        if(StrUtil.isNotEmpty(flowableTemplateEntity.getContextJson())){
            JSONObject jsonObject = JSONUtil.parseObj(flowableTemplateEntity.getContextJson());
            JSONObject process = jsonObject.getJSONObject("process");
            contentXml = FlowableUtils.jsonToXml(flowableTemplateEntity.getActKey(),flowableTemplateEntity.getTitle(),process);
        }
        flowableTemplateEntity.setContentXml(contentXml);
        this.updateById(flowableTemplateEntity);
    }

    @Override
    public List<FlowableTemplateDto> unbindFlowableTemplate() {
        return ConvertUtil.transformObjList(baseMapper.unbindFlowableTemplate(),FlowableTemplateDto.class);
    }

    @Override
    public boolean delById(Long id) {
        FlowableTemplateEntity flowableTemplateEntity = getById(id);
        if(removeById(id)){
            if(flowableTemplateEntity!=null && flowableTemplateEntity.getFormKey()!=null){
                flowableFormConfigService.removeById(flowableTemplateEntity.getFormKey());
            }
        }
        return true;
    }

    @Override
    public boolean delByAppIds(List<Long> ids) {
        if(ids!=null && ids.size()>0){
            flowableFormConfigService.removeByAppIds(ids);
            baseMapper.removeByAppIds(ids);
        }
        return false;
    }

    @Override
    public boolean isBind(Long id) {
        return baseMapper.isBind(id)>0;
    }

    @Override
    public String initActKey() {
        String actKey = null;
        QueryWrapper<FlowableTemplateEntity> queryWrapper=new QueryWrapper();
        List<FlowableTemplateEntity> list = null;
        do{
            actKey = CommonUtil.getRandomString(10,CommonUtil.RANGE4)+CommonUtil.getRandomString(10,CommonUtil.RANGE5);
            queryWrapper.eq("act_key",actKey);
            list = baseMapper.selectList(queryWrapper);
            if(list==null || list.size()<1){
                break;
            }
        }while(list.size()>0);
        return actKey;
    }

    @Override
    public List<FlowableTemplateDto> listFlowableTemplateIgnoreDel(FlowableTemplateDto temp) {
        return ConvertUtil.transformObjList(baseMapper.listFlowableTemplateIgnoreDel(ConvertUtil.transformObj(temp,FlowableTemplateEntity.class)),FlowableTemplateDto.class);
    }

    @Override
    public FlowableTemplateEntity getByIdIgnoreDel(String fid) {
        return baseMapper.getByIdIgnoreDel(fid);
    }

}

